package com.wchen.gdoc.generate;

import com.alibaba.fastjson.JSONObject;
import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.mapping.DefaultClassStructureResolver;
import com.wchen.gdoc.generate.mapping.data.ClassData;
import com.wchen.gdoc.generate.processor.DataProcessor;
import com.wchen.gdoc.generate.resource.DefaultResourceLoader;
import com.wchen.gdoc.generate.resource.ResourceLoader;
import com.wchen.gdoc.generate.resource.structure.ClassStructure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 文档生成管理
 *
 * @author  wchen
 * @date    2019/11/11
 */
public class DocumentGenerateManager {
    private final static Logger LOGGER = LoggerFactory.getLogger(DocumentGenerateManager.class);

    private static boolean DEBUG_ENABLE = ApplicationConfigProvide.getApplicationConfig().getCoreConfig().getDebugEnable();
    //资源加载器
    private ResourceLoader resourceLoader;
    //资源解析器
    private DefaultClassStructureResolver classStructureResolver;

    public DocumentGenerateManager() {
        this.resourceLoader = new DefaultResourceLoader();
        this.classStructureResolver = new DefaultClassStructureResolver();
    }

    /**
     * 启动
     */
    public void start(){
        List<ClassStructure> classStructures = this.resourceLoader.loadResource();
        List<ClassData> classDataList = this.classStructureResolver.resolveClassStructures(classStructures);
        if(classDataList.isEmpty()){
            LOGGER.info("classDataList is empty!");
            return;
        }
        if(DEBUG_ENABLE) {
            LOGGER.info(JSONObject.toJSONString(classDataList, true));
        }

        DataProcessor dataProcessor = ApplicationConfigProvide.getApplicationConfig().getDataProcessor();
        classDataList.forEach(classData -> dataProcessor.dealClassData(classData));
    }
}
